import unittest
import sys, os
from os.path import join, dirname
location = join(dirname(sys.argv[0]), '..')

sys.path.insert(0, location)

import port_command
import catcher

class PortQueryTest(unittest.TestCase):
    def setUp(self):
        self._path = "/opt/local/bin/port"
        
    def testCreation(self):
        pc = port_command.PortCommand(self._path)
        self.assertEquals(pc._path, self._path)
        self.assertEquals(pc._options, [])
        self.assertEquals(pc._target, port_command.targets.LIST)
        
    def testAddOption(self):
        pc = port_command.PortCommand(self._path)
        pc.addOption(port_command.options.DEBUG)
        pc.addOption(port_command.options.COMMANDS_FILE, 'foo')
        self.assertEquals(pc._options, 
                        [
                            port_command.options.DEBUG,
                            port_command.options.COMMANDS_FILE,
                            'foo'
                        ])

    def testClearOptions(self):7
        pc = port_command.PortCommand(self._path)
        pc.addOption(port_command.options.DEBUG)
        pc.clearOptions()
        self.assertEquals(pc._options, [])
        
    def testTarget(self):
        pc = port_command.PortCommand(self._path)        
        for target in port_command.targets.targets.values():
            try:
                pc.target = target
            except:
                # it should not throw exceptions
                self.fail("Exception thrown in testTarget where no exception was expected.")
        def setTarget(self, target):
            self.target = target
        self.failUnlessRaises(TypeError, setTarget, pc, 'invalid')
        
    def testList(self):
        pc = port_command.PortCommand(self._path)
        pc.list()
        s = pc.getAllData()
        # print pc.catcher.read() # we must setup a good testing 
                                # framework for this...
    
if __name__ == '__main__':
    unittest.main()
